//
//  LGVisitorView.swift
//  LGWeibo
//
//  Created by 李更 on 8/10/16.
//  Copyright © 2016 itcast. All rights reserved.
//

import UIKit
import SnapKit
class LGVisitorView: UIView {
    //声明闭包01
    var closure : (()->())?
    
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    //供外界调用的方法
    func setUpVisitor(imgName : String? , title : String?){
        //if判断如果不是首页
        if let img = imgName ,let tit = title {
            iconImageView.image = UIImage(named: img)
            messageLable.text = tit
            circleImageView.hidden = true
            
        }else{
        //代表首页
            setuoAnim()
        }
     
    }
    
    //visitor页面点击事件
    @objc private func buttonClick(){
        //03执行闭包
        closure?()
    }
    
    //设置圈子转圈
    func setuoAnim(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //设置toValue
        anim.toValue = M_PI * 2
        //设置时间
        anim.duration = 10
        //设置循环次数
        anim.repeatCount = MAXFLOAT
        //切换页面默认移除动画 
        anim.removedOnCompletion = false
        
        
        //添加动画
        circleImageView.layer.addAnimation(anim, forKey: nil)
        
        
    }
    
    
    //设置视图
    func setupUI(){
        backgroundColor = RGB(237, green: 237, blue: 237)
        //添加控件
        addSubview(circleImageView)
        addSubview(mask)
        addSubview(iconImageView)
        addSubview(messageLable)
        addSubview(registerBtn)
        addSubview(loginBtn)
        //添加约束
        //圆圈
        circleImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        //挡板
        mask.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        //房子
        iconImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
       
        //文字
        messageLable.snp_makeConstraints { (make) in
            make.top.equalTo(mask.snp_bottom).offset(16)
            make.height.equalTo(36)
            make.centerX.equalTo(self)
            
        }
        //注册按钮
        registerBtn.snp_makeConstraints { (make) in
            make.top.equalTo(messageLable.snp_bottom).offset(16)
            make.leading.equalTo(messageLable)
            make.size.equalTo(CGSizeMake(100, 36))
        }
        //登录按钮
        loginBtn.snp_makeConstraints { (make) in
            make.size.top.equalTo(registerBtn)
            make.trailing.equalTo(messageLable)
        }
        
    }
    
    
    
    
    //懒加载控件
    //圆圈
    private lazy var circleImageView : UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    //挡板
    private lazy var mask : UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    //房子
    private lazy var iconImageView : UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    //文字
    private lazy var messageLable : UILabel = {
        let lab = UILabel()
        //内容
        lab.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        //文字大小
        lab.font = UIFont.systemFontOfSize(15)
        //文字颜色
        lab.textColor = UIColor.orangeColor()
        //变行
        lab.numberOfLines = 0
        //居中
        lab.textAlignment = .Center
        //最大宽度
        lab.preferredMaxLayoutWidth = 240
        return lab
        
    }()
    //按钮
    private lazy var registerBtn : UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
        //设置背景图片
       btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        btn.setTitle("注册", forState: UIControlState.Normal)
        
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        return btn
        
    }()
    private lazy var loginBtn : UIButton = {
        let btn = UIButton()
         btn.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
        //设置背景图片
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        btn.setTitle("登录", forState: UIControlState.Normal)
        
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        return btn
        
    }()

    

    
    
    
    
    
    
}
